package com.itfreer.cms.service.content;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.cms.entity.content.DocumentEntity;
import com.itfreer.form.api.BaseRestService;
import com.itfreer.utils.json.JsonUtils;

@Component
@Path("/cms_document_show/")
public class RestDocumentShowService {

	@Autowired(required = false)
	private BaseRestService<DocumentEntity> baseRestService;
	
	@Autowired(required = false)
	private DocumentService documentService;
	
	/**
	 * 文档获取数据-对外
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/getEntitys")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public List<DocumentEntity> getEntitysDocument(String data) throws IOException {

		HashMap<String, Object> args = baseRestService.getRestParam(data);
		String queryFields = null;
		String whereString = null;
		String orderString = null;
		Integer pageSize = null;
		Integer pageIndex = 1;

		if (args != null) {
			if (args.containsKey("queryFields") && args.get("queryFields") != null)
				queryFields = args.get("queryFields").toString();
			if (args.containsKey("where") && args.get("where") != null)
				whereString = args.get("where").toString();
			if (args.containsKey("order") && args.get("order") != null)
				orderString = args.get("order").toString();
			if (args.containsKey("pageSize") && args.get("pageSize") != null)
				pageSize = new Integer(args.get("pageSize").toString());
			if (args.containsKey("pageIndex") && args.get("pageIndex") != null)
				pageIndex = new Integer(args.get("pageIndex").toString());
		}
		HashMap where = JsonUtils.toBean(whereString, HashMap.class);
		HashMap order = JsonUtils.toBean(orderString, HashMap.class);

		return documentService.getEntitys(queryFields, where, order, pageSize, pageIndex);
	}
	
	@POST
	@Path("/getEntity/{id:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public DocumentEntity getEntityDocument(@PathParam("id") String guid) {
		return documentService.getEntity(guid);
	};
	
	@POST
	@Path("/getRowCounts/{id:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public Long getRowCountsDocument(String data) throws IOException {
		
		HashMap args = baseRestService.getRestParam(data);
		String whereString = null;
		if (args != null) {
			if (args.containsKey("where") && args.get("where") != null)
				whereString = args.get("where").toString();
		}
		HashMap where = JsonUtils.toBean(whereString, HashMap.class);
		
		return documentService.getRowCounts(where);
		
	}
		
}
